package com.lijie.bs_recruit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lijie.bs_recruit.Util.WrapperUtil;
import com.lijie.bs_recruit.mapper.HrMapper;
import com.lijie.bs_recruit.mapper.PositionMapper;
import com.lijie.bs_recruit.model.Hr;
import com.lijie.bs_recruit.service.HrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 李杰
 * @since 2021-03-15
 */
@Service
public class HrServiceImpl extends ServiceImpl<HrMapper, Hr> implements HrService {

    @Autowired
    HrMapper hrMapper;

    @Autowired
    WrapperUtil wrapperUtil;
    @Autowired
    PositionMapper positionMapper;

    public Map<String,Object> login(String hruserName, String hrPassword){
        Map<String ,Object> result =new HashMap<>();
        int status=-1;
        //查询条件
        QueryWrapper<Hr> wrapper=new QueryWrapper<>();
        wrapper.eq("hruserName",hruserName);
        //通过username查询
        Hr rightHr = this.getOne(wrapper);
        if(rightHr!=null){
            //存在用户
            if(!rightHr.getHrPassword().equals(hrPassword)){
                status = 2;//密码错误
            }else{
                if(rightHr.getHrisDelete()!=1){//账号未停用
                    status=0;//登录成功
                    result.put("hr",rightHr);
                }else{//账号停用
                    status=4;
                }
            }
        }else {
            status=1;//不存在用户
        }
        result.put("status",status);
        return result;
    }

    @Override
    public Hr getHrByhrsername(String hruserName) {
        QueryWrapper<Hr> wrapper = new QueryWrapper<>();
        wrapper.eq("hruserName", hruserName);
        Hr hr = hrMapper.selectOne(wrapper);
        return hr;
    }

    @Override
    public PageInfo<Hr> selHrPage(Integer pageIndex, Hr hr) {
        QueryWrapper<Hr> businessQueryWrapper = wrapperUtil.wrapperHr(hr);
        PageHelper.startPage(pageIndex,8);
        PageInfo<Hr> pageInfo = new PageInfo<>(baseMapper.selectList(businessQueryWrapper));
        return pageInfo;
    }

    @Override
    public Boolean changeHrIdeleted(Hr hr) {
        Hr hr1 = baseMapper.selectById(hr.getHrId());
        UpdateWrapper<Hr> wrapper=new UpdateWrapper<>();
        if(hr1!=null){
            wrapper.set("hrisDelete",hr1.getHrisDelete()==0?1:0)
                    .eq("hrId", hr.getHrId());

        }
        int update = baseMapper.update(hr, wrapper);
        return update>0?true:false;
    }

    //更新Hr

    @Override
    public Boolean updateHr(Hr hr) {
        UpdateWrapper<Hr> wrapper = wrapperUtil.hrUpdateWrapper(hr);
        int update = baseMapper.update(hr, wrapper);
        return update>0;
    }
}
